package com.bingo.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bingo.common.R;
import com.bingo.dao.ImmunizationMapper;
import com.bingo.entity.CurPool;
import com.bingo.entity.Immunization;
import com.bingo.entity.Medication;
import com.bingo.entity.dto.resp.ImmunizationRecord;
import com.bingo.entity.dto.resp.ReportBasic;
import com.bingo.service.CurPoolService;
import com.bingo.service.ImmunizationService;
import com.bingo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Date;
import java.time.LocalDate;
import java.util.*;
import java.util.function.Function;

@Service
public class ImmunizationServiceImpl extends ServiceImpl<ImmunizationMapper, Immunization> implements ImmunizationService {
    private CurPoolService curPoolService;
    @Autowired
    public void setPrePoolService(CurPoolService curPoolService) {
        this.curPoolService = curPoolService;
    }

    private UserService userService;
    @Autowired
    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    @Override
    public List<ImmunizationRecord> selectImmunizationRecord(List<String> poolNos, Date startTime, Date endTime, String userId) {
        if (poolNos == null || poolNos.size() == 0){
            poolNos = curPoolService.getPoolNos();
        }

        QueryWrapper<Immunization> wrapper = new QueryWrapper<>();
        wrapper.in("pool_no", poolNos);
        wrapper.between("date", startTime, endTime);
        if (userId != null && !userId.equals("")){
            wrapper.eq("user_id", userId);
        }
        List<Immunization> list = this.list(wrapper);
        LinkedList<ImmunizationRecord> immunizationRecords = new LinkedList<>();
        HashMap<String, String> allUser = userService.getAllUser();

        for (Immunization immunization : list) {
            ImmunizationRecord immunizationRecord = new ImmunizationRecord();
            ReportBasic reportBasic = new ReportBasic();
            reportBasic.setPoolNo(immunization.getPoolNo());
            String s = allUser.get(immunization.getUserId());
            reportBasic.setReporter(s);
            reportBasic.setDate(immunization.getDate());
            reportBasic.setReportDate(immunization.getReportDate());
            immunizationRecord.setDescription(immunization.getDescription());
            immunizationRecord.setReportBasic(reportBasic);
            immunizationRecords.add(immunizationRecord);
        }
        return immunizationRecords;
    }

    @Override
    public boolean deleteImmunizationRecord(Immunization immunization) {
        if (immunization==null || immunization.getPoolNo()==null || immunization.getDate()==null ){
            return false;
        }
        QueryWrapper<Immunization> wrapper = new QueryWrapper<>();
        wrapper.eq("pool_no", immunization.getPoolNo());
        wrapper.eq("date", immunization.getDate());
        // 调用remove方法来删除记录
        return this.remove(wrapper);
    }
    // 新增防疫记录
    public R add(Immunization immunization) {
        //查询该塘
        CurPool pool = curPoolService.getCurPoolByNo(immunization.getPoolNo());

        if (pool==null){
            return R.error("该塘不存在！");
        }
        if (pool.getType()==0){
            return R.error("该塘是空塘！");
        }
        // 查询是否已经存在当天防疫记录
        QueryWrapper<Immunization> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("pool_no", immunization.getPoolNo());
        queryWrapper.eq("date", immunization.getDate());
        Immunization old = this.getOne(queryWrapper);

        // 获取当前日期
        LocalDate today = LocalDate.now();
        // 填充数据
        Immunization new_immunization = new Immunization();
        new_immunization.setUserId(immunization.getUserId());
        new_immunization.setPoolNo(immunization.getPoolNo());
        new_immunization.setDescription(immunization.getDescription());
        new_immunization.setDate(immunization.getDate());
        new_immunization.setReportDate(Date.valueOf(today));

        // 还未存在当天防疫记录
        if (old == null) {
            // 保存
            this.save(new_immunization);
            return R.success("添加成功");
        } else {
            return R.error("存在当天记录！");
        }
    }

    @Override
    public boolean update(Immunization immunization) {
        // 创建更新条件
        UpdateWrapper<Immunization> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("pool_no", immunization.getPoolNo());
        updateWrapper.eq("date", immunization.getDate());

        // 设置需要更新的字段
        updateWrapper.set("description", immunization.getDescription());

        // 执行更新操作
        return this.update(updateWrapper);

    }


}
